package com.tiantian.datajpa.service.impl;

import java.io.Serializable;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.tiantian.datajpa.service.GenericService;

public class GenericServiceImpl<T extends Serializable, PK extends Serializable> implements GenericService<T, PK> {

	private PagingAndSortingRepository<T, PK> genericDAO;

	public void setGenericDAO(PagingAndSortingRepository<T, PK> genericDAO) {
		this.genericDAO = genericDAO;
	}

	public T get(PK id) {
		if (id != null) {
			return genericDAO.findOne(id);
		} else {
			return null;
		}
	}

	public T save(T entity) {
		return genericDAO.save(entity);
	}

	public <S extends T> Iterable<S> save(Iterable<S> entities) {
		return genericDAO.save(entities);
	}

	public boolean exists(PK id) {

		return genericDAO.exists(id);
	}

	public Iterable<T> findAll() {
		return genericDAO.findAll();
	}

	public void delete(T entity) {
		genericDAO.delete(entity);
	}

	public void deleteByKey(PK id) {
		genericDAO.delete(id);
	}

	public void deleteAll(Iterable<T> entities) {
		genericDAO.delete(entities);
	}

	public Iterable<T> findAll(Sort sort) {
		return genericDAO.findAll(sort);
	}

	public Page<T> findAll(Pageable pageable) {
		return genericDAO.findAll(pageable);
	}

}
